﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Data;

namespace BanVeXemPhim_Presentation
{
    public class XuatChieuDAO :DataProvider
    {
        public XuatChieuDTO[] DanhSachLichChieuTheoPhim(PhimDTO phim)
        {
            connect();
            string query = "select * "+
                            "from xuatchieu "+
                            "where xuatchieu.phim="+
                            phim.IdPhim.ToString();

            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;

            XuatChieuDTO[] xuatChieuDtos=new XuatChieuDTO[n];
            for (int i = 0; i < n;i++ )
            {
                xuatChieuDtos[i] = (XuatChieuDTO)GetDataFromDataRow(dataTable, i);
            }

            return xuatChieuDtos;
        }
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            XuatChieuDTO xuatChieuDto=new XuatChieuDTO();
            xuatChieuDto.IdXuatChieu = Convert.ToInt32(dt.Rows[i]["IdXuatChieu"].ToString());           
            xuatChieuDto.IdTuan = Convert.ToInt32(dt.Rows[i]["IdTuan"].ToString());
            xuatChieuDto.Phim = Convert.ToInt32(dt.Rows[i]["Phim"].ToString());
            xuatChieuDto.NgayChieu = Convert.ToDateTime(dt.Rows[i]["NgayChieu"].ToString());
            xuatChieuDto.GioChieu = Convert.ToDateTime(dt.Rows[i]["GioChieu"].ToString());
            xuatChieuDto.Gio = xuatChieuDto.GioChieu.ToShortTimeString();
            xuatChieuDto.RapChieu = Convert.ToInt32(dt.Rows[i]["RapChieu"].ToString());
            if (dt.Rows[i]["PhanTramGiamGia"].ToString() == "")
            {
                xuatChieuDto.PhanTramGiamGia = 0;
            }
            else
                xuatChieuDto.PhanTramGiamGia = Convert.ToInt32(dt.Rows[i]["PhanTramGiamGia"].ToString());

            return xuatChieuDto;
        }


        public void ThemXuatChieu(XuatChieuDTO xuatChieuDto)
        {
            connect();
            string query = "insert into xuatchieu(IdTuan,Phim,NgayChieu,GioChieu,RapChieu,PhanTramGiamGia) values (@idtuan,@idphim,@ngaychieu,@giochieu,@rapchieu,@phantramgiamgia)";

            command = connection.CreateCommand();
            command.CommandText = query;

            SqlParameter parameter = command.Parameters.Add("@idtuan", SqlDbType.Int);
            parameter.Value = xuatChieuDto.IdTuan;

            parameter = command.Parameters.Add("@idphim", SqlDbType.Int);
            parameter.Value = xuatChieuDto.Phim;

            parameter = command.Parameters.Add("@ngaychieu", SqlDbType.DateTime);
            parameter.Value = xuatChieuDto.NgayChieu;

            parameter = command.Parameters.Add("@giochieu", SqlDbType.DateTime);
            parameter.Value = xuatChieuDto.GioChieu;

            parameter = command.Parameters.Add("@rapchieu", SqlDbType.Int);
            parameter.Value = xuatChieuDto.RapChieu;

            parameter = command.Parameters.Add("@phantramgiamgia", SqlDbType.Int);
            parameter.Value = xuatChieuDto.PhanTramGiamGia;

            command.ExecuteNonQuery();

        }

        public XuatChieuDTO[] DanhSachXuatChieuTheoTuan(LichChieuTuanDTO lichChieuTuanDto)
        {
            connect();
            string query = "select * from xuatchieu where idtuan="+lichChieuTuanDto.IdTuan;
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet);
            DataTable dataTable = dataSet.Tables[0];
            int n = dataTable.Rows.Count;
            XuatChieuDTO[] xuatChieuDtos = new XuatChieuDTO[n];
            for (int i = 0; i < n; i++)
            {
                xuatChieuDtos[i] = (XuatChieuDTO)GetDataFromDataRow(dataTable, i);
            }
            return xuatChieuDtos;
        }

        public void HuyXuatChieu(XuatChieuDTO xuatChieuDTO)
        {
            connect();
            string query = "delete from Xuatchieu where idxuatchieu="+xuatChieuDTO.IdXuatChieu;
            executeNonQuery(query);
        }

        public void CapNhatXuatChieu(XuatChieuDTO xuatChieuDto)
        {
            connect();
            string query = "update xuatchieu set phim=@phim,ngaychieu=@ngaychieu,giochieu=@giochieu,rapchieu=@rapchieu,phantramgiamgia=@giamgia";
            command = connection.CreateCommand();
            command.CommandText = query;
            SqlParameter parameter = command.Parameters.Add("@phim", SqlDbType.Int);
            parameter.Value = xuatChieuDto.Phim;

            parameter = command.Parameters.Add("@ngaychieu", SqlDbType.DateTime);
            parameter.Value = xuatChieuDto.NgayChieu;

            parameter = command.Parameters.Add("@giochieu", SqlDbType.DateTime);
            parameter.Value = xuatChieuDto.GioChieu;

            parameter = command.Parameters.Add("@rapchieu", SqlDbType.Int);
            parameter.Value = xuatChieuDto.RapChieu;

            parameter = command.Parameters.Add("@giamgia", SqlDbType.Int);
            parameter.Value = xuatChieuDto.PhanTramGiamGia;

            command.ExecuteNonQuery();

        }
    }
}
